/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.utils;

import cn.ac.nercis.pes.common.utils.JsonUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;

import java.util.Objects;

/**
 * 日志内容工具类
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public final class LogUtils {
    /**
     * 是否记录操作日志
     */
    @Getter
    @Setter
    private static boolean operationLog = true;

    private LogUtils() {
    }

    /**
     * 记录INFO级别日志
     *
     * @param log logger
     * @param msg the message string to be logged
     */
    public static void info(Logger log, String msg) {
        if (operationLog) {
            log.info(msg);
        }
    }

    /**
     * 记录INFO级别日志
     *
     * @param log    logger
     * @param format the format string
     * @param arg    the argument
     */
    public static void info(Logger log, String format, Object arg) {
        if (operationLog) {
            log.info(format, arg);
        }
    }

    /**
     * 记录请求数据
     *
     * @param log    logger
     * @param format the format string
     * @param arg    the data
     */
    public static void record(Logger log, String format, Object arg) {
        if (operationLog) {
            log.info(format, encryptContent(arg));
        }
    }

    /**
     * 记录请求数据
     *
     * @param log    logger
     * @param format the format string
     * @param arg1   the first argument
     * @param arg2   the second argument
     */
    public static void record(Logger log, String format, Object arg1, Object arg2) {
        if (operationLog) {
            log.info(format, encryptContent(arg1), encryptContent(arg2));
        }
    }

    /**
     * 记录请求数据
     *
     * @param log    logger
     * @param format the format string
     * @param arg1   the first argument
     * @param arg2   the second argument
     * @param arg3   the three argument
     */
    public static void record(Logger log, String format, Object arg1, Object arg2, Object arg3) {
        if (operationLog) {
            log.info(format, encryptContent(arg1), encryptContent(arg2), encryptContent(arg3));
        }
    }


    private static String encryptContent(Object content) {
        if (Objects.isNull(content)) {
            return StringUtils.EMPTY;
        }
        return JsonUtils.toJson(content);
    }
}